package com.hrbxlh.virtualization.dto;

import com.google.common.base.Objects;
import com.hrbxlh.virtualization.enums.ImageTypeEnum;
import com.hrbxlh.virtualization.vo.ChapterImageAllocationVO;
import com.hrbxlh.virtualization.vo.ImageAllocationVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

/**
 * @author cheer
 */
public class ImageAllocationDTO {

    private Integer chapterId;

    private String chapterName;

    private Boolean cluster;

    private Long imageId;

    private Long clusterId;

    private String flavor;

    private String imageName;

    private Boolean initial;

    private String userName;

    private String clusterName;

    private Boolean broken;

    public ImageAllocationVO convertToImageAllocationVO() {
        ImageAllocationVO imageAllocationVO = new ImageAllocationVO();
        BeanUtils.copyProperties(this, imageAllocationVO);
        if (cluster) {
            imageAllocationVO.setItemName("集群：" + clusterName + " " + generate(flavor));
        } else {
            imageAllocationVO.setItemName("镜像：" + imageName + " " + generate(flavor));
        }
        return imageAllocationVO;
    }

    private String generate(String flavor) {
        String[] strings = StringUtils.split(flavor, ",");
        if (strings == null) {
            return "";
        }
        if (strings.length == 2) {
            return strings[0] + "核" + strings[1] + "G";
        }
        if (strings.length == 6) {
            return strings[0] + "核" + strings[1] + "G" + strings[2] + "核" + strings[3] + "G" + strings[4] + "核"
                    + strings[5] + "G";
        }
        return "";
    }

    public ChapterImageAllocationVO convertToChapterImageAllocationVO() {
        ChapterImageAllocationVO chapterImageAllocationVO = new ChapterImageAllocationVO();
        BeanUtils.copyProperties(this, chapterImageAllocationVO);

        if (cluster == null) {
            chapterImageAllocationVO.setImageType("-");
            chapterImageAllocationVO.setBaseImageName("-");
            chapterImageAllocationVO.setImageSource("-");
            chapterImageAllocationVO.setBroken(false);
        } else if (cluster) {
            chapterImageAllocationVO.setImageType(ImageTypeEnum.CLUSTER.getMessage());
            chapterImageAllocationVO.setBaseImageName(clusterName);
            chapterImageAllocationVO.setImageSource("-");
        } else {
            chapterImageAllocationVO.setImageType(ImageTypeEnum.SINGLE.getMessage());
            chapterImageAllocationVO.setBaseImageName(imageName);
            if (initial) {
                chapterImageAllocationVO.setImageSource("预置");
            } else {
                chapterImageAllocationVO.setImageSource(userName);
            }
            chapterImageAllocationVO.setBroken(false);
        }

        return chapterImageAllocationVO;
    }

    public Integer getChapterId() {
        return chapterId;
    }

    public void setChapterId(Integer chapterId) {
        this.chapterId = chapterId;
    }

    public String getChapterName() {
        return chapterName;
    }

    public void setChapterName(String chapterName) {
        this.chapterName = chapterName;
    }

    public Boolean getCluster() {
        return cluster;
    }

    public void setCluster(Boolean cluster) {
        this.cluster = cluster;
    }

    public Long getImageId() {
        return imageId;
    }

    public void setImageId(Long imageId) {
        this.imageId = imageId;
    }

    public Long getClusterId() {
        return clusterId;
    }

    public void setClusterId(Long clusterId) {
        this.clusterId = clusterId;
    }

    public String getFlavor() {
        return flavor;
    }

    public void setFlavor(String flavor) {
        this.flavor = flavor;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public Boolean getInitial() {
        return initial;
    }

    public void setInitial(Boolean initial) {
        this.initial = initial;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public Boolean getBroken() {
        return broken;
    }

    public void setBroken(Boolean broken) {
        this.broken = broken;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImageAllocationDTO that = (ImageAllocationDTO) o;
        return Objects.equal(cluster, that.cluster) &&
                Objects.equal(imageId, that.imageId) &&
                Objects.equal(clusterId, that.clusterId) &&
                Objects.equal(flavor, that.flavor);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(cluster, imageId, clusterId, flavor);
    }
}
